package springcloud.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author: LeeRui
 * 2021/1/17 下午 1:11
 * consumer端通过eureka调用producer controller服务
 **/
@RestController
@RefreshScope
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private EurekaClient eurekaClient;

    @GetMapping("/test")
    public String test() {
//        //1.从eureka上获取PRODUCER服务信息
//        InstanceInfo producer = eurekaClient.getNextServerFromEureka("PRODUCER", false);
//        //2.获取PRODUCERip和端口
//        String hostName = producer.getHostName();
//        int port = producer.getPort();
//        //3.调用PRODUCER服务
//        String o = restTemplate.getForObject("http://" + hostName + ":" + port + "/producer/test", String.class);

        return restTemplate.getForObject("http://PRODUCER/producer/test", String.class);
    }

    @Value("${spring.application.name}")
    private String appName;

    @Value("${version}")
    private String version;

    @GetMapping("/version")
    public String verion(){
        return version;
    }


    @GetMapping("/test1")
    public String test1(@RequestParam(value = "name") String name,@RequestParam(value = "age") Integer age){

        return name+"======"+age+"======"+appName;
    }
}
